"""
上证指数(SH:000001)
https://stock.xueqiu.com/v5/stock/quote.json?symbol=SH000001&extend=detail
https://stock.xueqiu.com/v5/stock/chart/minute.json?symbol=SH000001&period=1d
https://xueqiu.com/hq#
"""

import requests,json,re,urllib3,time
urllib3.disable_warnings()
session = requests.Session()
get_url = 'https://xueqiu.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
}
TXTtemp = session.get(get_url,headers=headers,verify=False)
cookies = 'xq_a_token=' + str(session.cookies['xq_a_token']) + '; xqat='+ str(session.cookies['xqat']) + '; xq_r_token='+ str(session.cookies['xq_r_token']) + '; xq_id_token='+ str(session.cookies['xq_id_token']) + '; u='+ str(session.cookies['u'])

post_url = 'https://stock.xueqiu.com/v5/stock/quote.json?symbol=SH000001&extend=detail'
header = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  'Accept-Encoding': 'gzip, deflate, br',
  'Accept-Language': 'zh-CN,zh;q=0.9',
  'Cache-Control': 'max-age=0',
  'Connection': 'keep-alive',
  'Cookie': cookies,
  'Host': 'stock.xueqiu.com',
  'Sec-Fetch-Dest': 'document',
  'Sec-Fetch-Mode': 'navigate',
  'Sec-Fetch-Site': 'none',
  'Sec-Fetch-User': '?1',
  'Upgrade-Insecure-Requests': '1',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',
  'Content-Type': 'text/plain'
}

payload = "symbol=SH000001&extend=detail"
response=session.get(post_url,headers=header,data=payload,verify=False)
json_results = json.loads(response.text)
print(json_results['data']['market']['status'])

print(json_results['data']['quote']['name'] , json_results['data']['quote']['exchange'],json_results['data']['quote']['code'] )

print(json_results['data']['quote']['current'], json_results['data']['quote']['chg'], json_results['data']['quote']['percent'])

url = "https://xueqiu.com/service/v5/stock/screener/quote/list?type=sha&order_by=percent&order=desc&size=10&page=1&_=1582960697169"

payload = "type=sha&order_by=percent&order=desc&size=10&page=1&_=1582960697169"
headers = {
  'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate, br',
  'Accept-Language': 'zh-CN,zh;q=0.9',
  'cache-control': 'no-cache',
  'Connection': 'keep-alive',
  'Cookie': cookies,
  'Host': 'xueqiu.com',
  'Referer': 'https://xueqiu.com/hq',
  'Sec-Fetch-Dest': 'empty',
  'Sec-Fetch-Mode': 'cors',
  'Sec-Fetch-Site': 'same-origin',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',
  'X-Requested-With': 'XMLHttpRequest',
  'Content-Type': 'text/plain'
}

responsess = session.get(url, headers=headers, data = payload,verify=False)
json_resultss = json.loads(responsess.text)
print('股票	当前价	涨跌额	涨跌幅')
print(json_resultss['data']['list'][0]['name'],json_resultss['data']['list'][0]['current'],json_resultss['data']['list'][0]['chg'],json_resultss['data']['list'][0]['percent'])
print(json_resultss['data']['list'][1]['name'],json_resultss['data']['list'][1]['current'],json_resultss['data']['list'][1]['chg'],json_resultss['data']['list'][1]['percent'])
print(json_resultss['data']['list'][2]['name'],json_resultss['data']['list'][2]['current'],json_resultss['data']['list'][2]['chg'],json_resultss['data']['list'][2]['percent'])
print(json_resultss['data']['list'][3]['name'],json_resultss['data']['list'][3]['current'],json_resultss['data']['list'][3]['chg'],json_resultss['data']['list'][3]['percent'])
print(json_resultss['data']['list'][4]['name'],json_resultss['data']['list'][4]['current'],json_resultss['data']['list'][4]['chg'],json_resultss['data']['list'][4]['percent'])
print(json_resultss['data']['list'][5]['name'],json_resultss['data']['list'][5]['current'],json_resultss['data']['list'][5]['chg'],json_resultss['data']['list'][5]['percent'])
print(json_resultss['data']['list'][6]['name'],json_resultss['data']['list'][6]['current'],json_resultss['data']['list'][6]['chg'],json_resultss['data']['list'][6]['percent'])
print(json_resultss['data']['list'][7]['name'],json_resultss['data']['list'][7]['current'],json_resultss['data']['list'][7]['chg'],json_resultss['data']['list'][7]['percent'])
print(json_resultss['data']['list'][8]['name'],json_resultss['data']['list'][8]['current'],json_resultss['data']['list'][8]['chg'],json_resultss['data']['list'][8]['percent'])
print(json_resultss['data']['list'][9]['name'],json_resultss['data']['list'][9]['current'],json_resultss['data']['list'][9]['chg'],json_resultss['data']['list'][9]['percent'])